package com.csy.ocmis.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csy.ocmis.entity.Registeredtype;
import com.csy.ocmis.entity.Report;
import com.csy.ocmis.entity.ReportVo;
import com.csy.ocmis.service.IReportService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Time;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器 用户挂号
 * </p>
 *
 * @author csy
 * @since 2020-11-29
 */
@Controller
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private IReportService reportService;

    //判断就诊用户电话身份证号唯一
    @RequestMapping("phone")
    @ResponseBody
    public Object phone(String phone,String carid){
        int c = reportService.checkPhone(phone);
        int p = reportService.checkCarId(carid);
        if(c>=1){
            return 1;
        }else if(p>=1){
            return 2;
        }else{
            return 3;
        }
    }
    //查询挂号单
    @RequestMapping("index")
    public String toReport(ReportVo reportVo, Model model, String params, Integer cc, HttpServletRequest request){
        reportVo.setCc(cc);
        HttpSession session = request.getSession();
        //将数据存储到session中
        session.setAttribute("ban",cc);
        reportService.upddang();
        String reportName=params;
        reportVo.setReportName(reportName);
        List<ReportVo> sel = reportService.sel(reportVo);
        model.addAttribute("report",sel);
        return "cao/report";
    }
    //添加挂号单
    //添加新的挂号
    @RequestMapping("addre")
    @ResponseBody
    public Object addre(Report cReport){
        cReport.setState(1);
        boolean addre = reportService.save(cReport);
        if(addre){
            return "添加成功";
        }else{
            return "添加失败，请联系管理员";
        }
    }
    //删除挂号单
    @RequestMapping("delre")
    @ResponseBody
    public Object delre(Integer reportId){
        boolean addre = reportService.removeById(reportId);
        return addre;
    }

    //查询用户有没有填写病因
    @RequestMapping("selbing")
    @ResponseBody
    public Object selbing(Integer reid){
        return reportService.getById(reid).getZhuan();
    }
    //添加用户病因
    @RequestMapping("addbing")
    @ResponseBody
    public Object addbing(Integer reid, String bing, Report cReport){
        cReport.setZhuan(bing);
        cReport.setReportId(reid);
        Integer addbing = reportService.updateRe(cReport);
        return addbing;
    }


    //处方s收费
    @RequestMapping("shoufei")
    @ResponseBody
    public Object shoufei(ReportVo reportVo){
        Integer shoufei = reportService.shoufei(reportVo);
        //把挂号费添加到收费表上
        Integer guafei = reportService.guafei(reportVo);
        return  shoufei;
    }

    //查看完成挂号患者信息
    @RequestMapping("selhuan")
    @ResponseBody
    public Object selhuan(ReportVo reportVo,Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        List<ReportVo> selhuan = reportService.selhuan(reportVo);
        PageInfo pageInfo = new PageInfo(selhuan);
        Map<String, Object> tableData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式，如果后台没有加上这句话的话需要在前台页面手动设置
        tableData.put("code", 0);
        tableData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", pageInfo.getList());
        return tableData;
    }
}

